var LampBtnOn = false;
//获取灯开启和关闭的数量
var LampSum;
var lampOnNum;
var lampOffNum;
function lampSwitchInfo(datas) {
    var devicecode = datas.device.devCode;
    var latitude = datas.device.latitude;
    var longitude = datas.device.longitude;
    var deviceStatus = datas.status;
    var deviceName = datas.device.deviceName;
    var address = datas.device.address;
    var returnTimes = datas.returnTime;
    var lampSwitchHtml = '<li class="li_device" devicecode="' + devicecode + '"' +
        ' latitude = "' + latitude + '"' +
        ' longitude = "' + longitude + '"' +
        ' deviceStatus = "' + deviceStatus + '"' +
        ' deviceName = "' + deviceName + '"' +
        ' returnTimes = "' + returnTimes + '"' +
        ' address = "' + address + '"' +
        '><a href="#"><div class="pull-left"><i class="fa fa-map-marker"></i></div><h4 class="deviceName" style="margin-bottom:12px">' + deviceName + '<small><i class="fa fa-clock-o"></i>' + moment(returnTimes).format("YYYY-MM-DD HH:mm:ss") + '</small></h4><p>' + address + '</p></a></li>';

    $('.lampSwitch .menu li[devicecode=' + devicecode + ']').remove();
    if (deviceStatus == 20) {
        /*火警*/
        $('.streetLampOn .menu').prepend(lampSwitchHtml);
        $('.streetLampOn .menu .fa-map-marker').css("color", "#ff5656")
    } else if (deviceStatus == 24) {
        /*故障*/
        $('.streetLampOff .menu').prepend(lampSwitchHtml);
        $('.streetLampOff .menu .fa-map-marker').css("color", "#ff5656")
    }
}
function getLampSwitchNum() {
    $.ajax({
        url: '/deviceStatus/count/bystatus/24/bydevicetype/004/byuser',
        type: 'get',
        async: false,
        success: function (result) {
            if (result.status == 200) {
                lampOffNum = result.data;
            } else {
                winTip.alert(result.message)
            }
        }
    });
    $.ajax({
        url: '/deviceStatus/count/bystatus/20/bydevicetype/004/byuser',
        type: 'get',
        async: false,
        success: function (result) {
        	console.log(result)
            if (result.status == 200) {
                lampOnNum = result.data;
            } else {
                winTip.alert(result.message)
            }
        }
    });
    $.ajax({
        url: '/deviceStatus/count/bydevicetype/004/byuser',
        type: 'get',
        async: false,
        success: function (result) {
            if (result.status == 200) {
            	console.log(result)
                LampSum = result.data;
            } else {
                winTip.alert(result.message)
            }
        }
    });
//	控制灯头部的下拉菜单
    $.ajax({
        url: '/deviceStatus/data/list/bystatus/20/bydevicetype/004/byuser',
        type: 'GET',
        async: false,
        success: function (data) {
            if (data.status == 200) {
                for (var i = 0; i < data.data.length; i++) {
                    var datas = data.data[i];
                    $('.streetLampOn .menu').prepend(lampSwitchInfo(datas));
                    $('.streetLampOn .menu .fa-map-marker').css("color", "#ff5656")
                }
            } else {
                winTip.alert(data.message)
            }
        }
    });
    $.ajax({
        url: '/deviceStatus/data/list/bystatus/24/bydevicetype/004/byuser',
        type: 'GET',
        async: false,
        success: function (data) {
            if (data.status == 200) {
                for (var i = 0; i < data.data.length; i++) {
                    var datas = data.data[i];
                    $('.streetLampOff .menu').prepend(lampSwitchInfo(datas));
                    $('.streetLampOff .menu .fa-map-marker').css("color", "#ff5656")
                }
            } else {
                winTip.alert(data.message)
            }
        }
    });
    $(".streetLampOnNum").text(lampOnNum)
    $(".streetLampOffNum").text(lampOffNum)
    $(".streetLampSum span").text(LampSum)
    if (lampOnNum == LampSum) {
        //全开
        $("#LampBtnSumOpen").addClass("openStyle").removeClass("switchSumOff");
        $("#LampBtnSumClose").removeClass("closeStyle").addClass("switchSumOff");
    } else if (lampOffNum == LampSum) {
        //全关
        $("#LampBtnSumOpen").removeClass("openStyle").addClass("switchSumOff");
        $("#LampBtnSumClose").removeClass("switchSumOff").addClass("closeStyle");

    } else {
        //有开有关
        $("#LampBtnSumOpen").removeClass("openStyle").addClass("switchSumOff");
        $("#LampBtnSumClose").removeClass("closeStyle").addClass("switchSumOff");
    }

}


//点击单个路灯开关lampSwitchFunc
function lampSwitchFunc() {
    var timeoutflag = null;
    LampBtn1.onclick = function () {
//
        var $this = $(this)
        if (timeoutflag != null) {
            clearTimeout(timeoutflag);
            timeoutflag = null;
        } else {
            timeoutflag = setTimeout(function () {
                var longitude = $this.attr("longitude");
                var latitude = $this.attr("latitude");
                var no = $this.attr("devCode");
                var position = $this.attr("position");
                var deviceName = $this.attr("deviceName");
                var status = $this.attr("status");
                var linkMain1 = $this.attr("linkMain1");
                var linkMain2 = $this.attr("linkMain2");
                var linkMain3 = $this.attr("linkMain3");
                var linkMain4 = $this.attr("linkMain4");
                var linkPhone1 = $this.attr("linkPhone1");
                var linkPhone2 = $this.attr("linkPhone2");
                var linkPhone3 = $this.attr("linkPhone3");
                var linkPhone4 = $this.attr("linkPhone4");
                //开灯
                var lampStatusUrl = null;
//                LampBtn1.className = (LampBtn1.className == "close1") ? "open1" : "close1";
//                LampBtn2.className = (LampBtn2.className == "close2") ? "open2" : "close2";
                if (!$this.hasClass("open1")) {
                    LampBtnOn = false;
                    lampStatusUrl = 20;
//                    BaiDuMap.deletePoint(no);
//                    BaiDuMap.streetLampAddDot(longitude, latitude, no, deviceName, position, "streetLampOn", linkMain1, linkPhone1, linkMain2, linkPhone2, linkMain3, linkPhone3, linkMain4, linkPhone4);
                } else {
                    LampBtnOn = true;
                    //关灯
                    lampStatusUrl = 24;
//                    BaiDuMap.deletePoint(no);
//                    BaiDuMap.streetLampAddDot(longitude, latitude, no, deviceName, position, "streetLampOff", linkMain1, linkPhone1, linkMain2, linkPhone2, linkMain3, linkPhone3, linkMain4, linkPhone4);
                }
                //发送路灯数据给后台
                $.ajax({
                    url: '/light/contorller/' + no + '/' + lampStatusUrl,
                    type: 'get',
                    async: false,
                    success: function (result) {
                        if (result.status == 200) {
                            LampBtn1.className = (LampBtn1.className == "close1") ? "open1" : "close1";
                            LampBtn2.className = (LampBtn2.className == "close2") ? "open2" : "close2";
                        	if($this.hasClass("open1")){
                        	    BaiDuMap.deletePoint(no);
                                BaiDuMap.streetLampAddDot(longitude, latitude, no, deviceName, position, "streetLampOn", linkMain1, linkPhone1, linkMain2, linkPhone2, linkMain3, linkPhone3, linkMain4, linkPhone4);
                            } else {
                                BaiDuMap.deletePoint(no);
                                BaiDuMap.streetLampAddDot(longitude, latitude, no, deviceName, position, "streetLampOff", linkMain1, linkPhone1, linkMain2, linkPhone2, linkMain3, linkPhone3, linkMain4, linkPhone4);
                            }
//                            //渲染路灯数量
                            setTimeout(function () {
                            	getLampSwitchNum();
                            },0);
                        } else {
                            winTip.alert(result.message)
                        }
                    }
                });

                //渲染路灯数量
//                setTimeout(function () {
//                	getLampSwitchNum();
//                },0);
                timeoutflag = null;
            }, 0);
        }
    }
    return false;
}


//点击开关总按钮
function lampBtnSumFunc() {
    //全部关
    $("#LampBtnSumClose").click(function () {
        if (!$(this).hasClass("closeStyle")) {
        	//var LampBtnSumCloseClick = false;
            var lampDate = {};
            var devCodesArr = [];
            var devCodes = '';
            var lampSwitchHtml = '';

            $(".lampSwitch .li_device").each(function (i, item) {
                devCodesArr[i] = $(this).attr("devicecode");
                devCodes = devCodes + devCodesArr[i] + "-";
                var devicecode = $(this).attr("devicecode");
                var latitude = $(this).attr("latitude");
                var longitude = $(this).attr("longitude");
                var deviceStatus = $(this).attr("deviceStatus");
                var deviceName = $(this).attr("deviceName");
                var address = $(this).attr("address");
                var returnTimes = $(this).attr("returnTimes");
                lampSwitchHtml = '<li class="li_device" devicecode="' + devicecode + '"' +
                    ' latitude = "' + latitude + '"' +
                    ' longitude = "' + longitude + '"' +
                    ' deviceStatus = "' + deviceStatus + '"' +
                    ' deviceName = "' + deviceName + '"' +
                    ' returnTimes = "' + returnTimes + '"' +
                    ' address = "' + address + '"' +
                    '><a href="#"><div class="pull-left"><i class="fa fa-map-marker"></i></div><h4 class="deviceName" style="margin-bottom:12px">' + deviceName + '<small><i class="fa fa-clock-o"></i>' + moment(returnTimes).format("YYYY-MM-DD HH:mm:ss") + '</small></h4><p>' + address + '</p></a></li>';
                //先将原来的下拉列表移除
                $('.lampSwitch .menu li[devicecode=' + devicecode + ']').remove();
                $('#streetLampOff .menu').prepend(lampSwitchHtml);
//                $('.streetLampOff .menu .fa-map-marker').css("color", "#ff5656");
            });
                //将设备号拼接成字符串
                devCodes = devCodes.substring(0, devCodes.length - 1);
                lampDate = '{"cmd":"24","devCodes":"' + devCodes + '"}';
                console.log(lampDate)
            

//			发送所有路灯关闭命令
            $.ajax({
                url: '/lights/contorller',
                type: 'post',
                data: lampDate,
                contentType: "application/json",
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.status == 200) {
                    	//LampBtnSumCloseClick = true;
                        console.log(result)
                    } else {
                        winTip.alert(result.message)
                    }
                }
            });

            //if(LampBtnSumCloseClick){
                $(this).removeClass("switchSumOff").addClass("closeStyle");
                $("#LampBtnSumOpen").removeClass("openStyle").addClass("switchSumOff");
                //将所有数据渲染成关闭状态
                $.ajax({
                    url: '/deviceStatus/data/list/bydevicetype/004',
                    type: 'get',
                    async: false,
                    success: function (result) {
                        if (result.status == 200) {
                            for (var ri = 0; ri < result.data.length; ri++) {
                                var r = result.data[ri];
                                //20->1号   21->2号 24关   20->开
                                BaiDuMap.deletePoint(r.device.devCode);
                                BaiDuMap.streetLampAddDot(r.device.latitude, r.device.longitude, r.device.devCode, r.device.deviceName, r.device.address, 'streetLampOff', r.device.linkMain1, r.device.linkPhone1, r.device.linkMain2, r.device.linkPhone2, r.device.linkMain3, r.device.linkPhone3, r.device.linkMain4, r.device.linkPhone4);
                            }
                        } else {
                            winTip.alert(result.message)
                        }
                    }
                });
//    		  	更改路灯开关数量
                getLampSwitchNum();
                $(".streetLampOffNum").text(LampSum);
                $(".streetLampOnNum").text(0);
    //按钮全关
                LampBtn1.className = "close1";
                LampBtn2.className = "close2";
            //}
        }
       // return false;
    });

    $("#LampBtnSumOpen").click(function () {
    	var $this = $(this);
        //全部开
        if (!$this.hasClass("openStyle")) {
        	//var LampBtnSumOpenClick = false;
            var lampDate = {};
            var devCodesArr = [];
            var devCodes = '';
            var lampSwitchHtml = "";
            
            $(".lampSwitch .li_device").each(function (i, item) {
                devCodesArr[i] = $(this).attr("devicecode");
                devCodes = devCodes + devCodesArr[i] + "-";
                var devicecode = $(this).attr("devicecode");
                var latitude = $(this).attr("latitude");
                var longitude = $(this).attr("longitude");
                var deviceStatus = $(this).attr("deviceStatus");
                var deviceName = $(this).attr("deviceName");
                var address = $(this).attr("address");
                var returnTimes = $(this).attr("returnTimes");
                lampSwitchHtml = '<li class="li_device" devicecode="' + devicecode + '"' +
                    ' latitude = "' + latitude + '"' +
                    ' longitude = "' + longitude + '"' +
                    ' deviceStatus = "' + deviceStatus + '"' +
                    ' deviceName = "' + deviceName + '"' +
                    ' returnTimes = "' + returnTimes + '"' +
                    ' address = "' + address + '"' +
                    '><a href="#"><div class="pull-left"><i class="fa fa-map-marker"></i></div><h4 class="deviceName" style="margin-bottom:12px">' + deviceName + '<small><i class="fa fa-clock-o"></i>' + moment(returnTimes).format("YYYY-MM-DD HH:mm:ss") + '</small></h4><p>' + address + '</p></a></li>';
                //先将原来的下拉列表移除
                $('.lampSwitch .menu li[devicecode=' + devicecode + ']').remove();
                $('#streetLampOn .menu').prepend(lampSwitchHtml);
//                $('.streetLampOn .menu .fa-map-marker').css("color", "#ff5656");
            });
            //将设备号拼接成字符串
            devCodes = devCodes.substring(0, devCodes.length - 1);
            lampDate = '{"cmd":"20","devCodes":"' + devCodes + '"}';
            console.log(lampDate)
            
            //发送路灯全部开的命令
            $.ajax({
                url: '/lights/contorller',
                type: 'post',
                data: lampDate,
                dataType: 'json',
                contentType: "application/json",
                async: false,
                success: function (result) {
                    if (result.status == 200) {
                    	//LampBtnSumOpenClick = true;
                    } else {
                        winTip.alert(result.message)
                    }
                }
            });
            //if(LampBtnSumOpenClick){
                $this.removeClass("switchSumOff").addClass("openStyle");
                $("#LampBtnSumClose").removeClass("closeStyle").addClass("switchSumOff");
                //将所有数据渲染成关闭状态
                $.ajax({
                    url: '/deviceStatus/data/list/bydevicetype/004',
                    type: 'get',
                    success: function (result) {
                        if (result.status == 200) {
                            for (var ri = 0; ri < result.data.length; ri++) {
                                var r = result.data[ri];
                                //20->1号   21->2号 24关   20->开
                                BaiDuMap.deletePoint(r.device.devCode);
                                BaiDuMap.streetLampAddDot(r.device.latitude, r.device.longitude, r.device.devCode, r.device.deviceName, r.device.address, 'streetLampOn', r.device.linkMain1, r.device.linkPhone1, r.device.linkMain2, r.device.linkPhone2, r.device.linkMain3, r.device.linkPhone3, r.device.linkMain4, r.device.linkPhone4);
                            }
                        } else {
                            winTip.alert(result.message)
                        }
                    }
                });
                getLampSwitchNum();
                $(".streetLampOnNum").text(LampSum);
                $(".streetLampOffNum").text(0);
               // $("body").on("click", "#LampBtn1", lampSwitchFunc());
//    			按钮全开
                LampBtn1.className = "open1";
                LampBtn2.className = "open2";
            //}

        }
        //return false;
    });
}


